package com.kgc.rb.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kgc.rb.common.tools.EmptyUtils;
import com.kgc.rb.dto.RbResult;
import com.kgc.rb.exception.UserExecption;
import com.kgc.rb.pojo.QgUser;
import com.kgc.rb.service.LocalUserService;
import com.kgc.rb.service.QgUserService;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private LocalUserService userService;

    @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
    public RbResult doLogin(String phone, String password) {


        if (EmptyUtils.isEmpty(phone)) {
            return RbResult.build(UserExecption.PHONE_IS_NULL.getCode(), UserExecption.PHONE_IS_NULL.getMessage());
        }
        if (EmptyUtils.isEmpty(password)) {
            return RbResult.build(UserExecption.PASSWORD_IS_NULL.getCode(), UserExecption.PASSWORD_IS_NULL.getMessage());
        }
        try {
            String token = userService.login(phone, password);
            //表示登录成功
            if (EmptyUtils.isNotEmpty(token)) {
                return RbResult.ok(token);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RbResult.build(UserExecption.USER_PASSWORD_ERROR.getCode(), UserExecption.USER_PASSWORD_ERROR.getMessage());
    }

    @RequestMapping(value = "/v/doLoginOut", method = RequestMethod.POST)
    public RbResult doLoginOut(String token) {
        return userService.loginOut(token);
    }
}